﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.Threading;

namespace Dev4Side.DbAttachWebPartIdCalculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btCalculateId_Click(object sender, EventArgs e)
        {
            try
            {
                // thx to: VanceK
                // http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/a0fc0978-0d55-463a-b7ab-72c00aee76a3
                string data = tbAssembly.Text;
                // Will use reflection to pull up WebPart subclasses
                // but for now just use this 1 test case
                data += "|" + tbTypeName.Text;
                byte[] dataBytes = Encoding.Unicode.GetBytes(data);
                MD5 hashAlgo = new MD5CryptoServiceProvider();
                byte[] hash = hashAlgo.ComputeHash(dataBytes);
                Guid hashGuid = new Guid(hash);
                tbResult.Text = hashGuid.ToString();
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Exception:");
                sb.AppendLine(String.Concat("Message: ", ex.Message));
                sb.AppendLine(String.Concat("StackTrace: ", ex.StackTrace));
                MessageBox.Show(sb.ToString());
            }
        }

        private void tbClearAssemblyTextBox_Click(object sender, EventArgs e)
        {
            tbAssembly.Text = string.Empty;
        }

        private void tbClearTypeName_Click(object sender, EventArgs e)
        {
            tbTypeName.Text = string.Empty;
        }

        private void tbCopyToClipBoard_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(tbResult.Text, true);
        }
    }
}
